﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SDT.Libraries.Utilities;
using System.Threading;

namespace TestPanel
{
   public class AsynTag2
    {
        public static void TimeoutAsyncDelegate(int idNews, string contentNews)
        {
            AsyncActionTag asyTag = new AsyncActionTag();
            asyTag.TimeOutWakeAsyncDelegate(idNews, contentNews);
        }

        public class AsyncActionTag
        {
            public void TimeOutWakeAsyncDelegate(int idNews, string contentNews)
            {
                AsyncInvoke GetTagMethod = new AsyncInvoke(TagAsyncInvoke.TagNLP);
                AsyncCallback callBack = new AsyncCallback(TagAsyncInvoke.CallBack);
                IAsyncResult asyncResult = GetTagMethod.BeginInvoke(idNews, contentNews, callBack, GetTagMethod);
                //while (!asyncResult.IsCompleted)
                //{
                //    // give up the CPU for 1 second
                //    Thread.Sleep(1000);
                //    Console.Write('.');
                //}
                //Console.WriteLine("Finished Polling");

                //try
                //{
                //    int retVal = GetTagMethod.EndInvoke(asyncResult);
                //    if (retVal > 0)
                //        new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertTagRankingThreading", idNews.ToString() + " is inserted"));

                //}
                //catch (Exception e)
                //{
                //    new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertTagRankingThreading", idNews.ToString() + " is timeout"));
                //}
                int counter = 0;
                while (counter <= 25 && !asyncResult.AsyncWaitHandle.WaitOne(6*1000, true))
                {
                    counter++;
                }

                if (asyncResult.IsCompleted)
                {
                    int retVal = GetTagMethod.EndInvoke(asyncResult);
                    if (retVal > 0)
                        new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertTagRankingThreading", idNews.ToString() + " is inserted"));
                }
                else
                {
                    new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertTagRankingThreading", idNews.ToString() + " is timeout"));
                }
                asyncResult.AsyncWaitHandle.Close();
            }
        }

        public delegate int AsyncInvoke(int idNews, string contentNews);

        public class TagAsyncInvoke
        {
            public static int TagNLP(int idNews, string contentNews)
            {
                TagWS.HelloService wsTag = new TagWS.HelloService();
                string resultTag = wsTag.getTagNPL(contentNews, 3);
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "InsertTagRankingResult", idNews.ToString()+"--------" + resultTag));
                return 1;
            }

            public static void CallBack(IAsyncResult ar)
            {
                AsyncInvoke ai = (AsyncInvoke)ar.AsyncState;
                int retVal = ai.EndInvoke(ar);
            }
        }
    }
}
